/**
 * 2009-11-3 上午09:29:22 
 * esignature
 * com.smc.action.document
 * DocumentAction
 */
package com.smc.action.document;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;
import com.smc.domain.model.Document;
import com.smc.domain.model.Hospital;
import com.smc.domain.model.User;
import com.smc.security.SecurityContextHolder;
import com.smc.server.DocumentService;

/**
 * @author chenheng
 * @version 0.0.1
 * 表现层文档操作
 */
public class DocumentAction extends ActionSupport implements ServletRequestAware {

	private static final long serialVersionUID = -8099577225045983535L;
	/**
	 * 文档列表
	 */
	private List<Document> documentList ;
	/**
	 * 据有医生角色的用户列表
	 */
	private List<User> doctorList ;
	/**
	 * 业务层文档服务
	 */
	private DocumentService documentService ;
	/**
	 * PdfDocument是对未进入系统数据库的文档的封装，具体信息参见此类的文档。这里是Pdf文档列表
	 */
	private List<PdfDocument> pdfDocumentList ;
	/**
	 * 通过Struts2框架自动注入Request对象
	 */
	private HttpServletRequest request ;
	/**
	 * 文档
	 */
	private Document document ;
	/**
	 * 收录文档时间
	 */
	private String receivedTime ;
	/**
	 * 文档签名时，用户输入签名原因
	 */
	private String reason ;
	/**
	 * 文档签名时，用户所在位置
	 */
	private String location ;
	
	//////////////////////////////////////////////////////////////////////
	///////////////  属性的基本setter、getter 访求 ///////////////////////////
	//////////////////////////////////////////////////////////////////////
	public void setServletRequest(HttpServletRequest request) {
		this.request = request ;
	}

	public List<User> getDoctorList() {
		return doctorList;
	}
	public void setDoctorList(List<User> doctorList) {
		this.doctorList = doctorList;
	}
	public List<Document> getDocumentList() {
		return documentList;
	}
	public void setDocumentList(List<Document> documentList) {
		this.documentList = documentList;
	}
	public DocumentService getDocumentService() {
		return documentService;
	}
	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
	}
	
	public List<PdfDocument> getPdfDocumentList() {
		return pdfDocumentList;
	}
	public void setPdfDocumentList(List<PdfDocument> pdfDocumentList) {
		this.pdfDocumentList = pdfDocumentList;
	}
	
	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public String getReceivedTime() {
		return receivedTime;
	}

	public void setReceivedTime(String receivedTime) {
		this.receivedTime = receivedTime;
	}
	
	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}
	///////////////////////////////////////////////////////////////////////////
	///////////////  表现层业务控制方法	///////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////
	/**
	 * 医生列出所有签名文档（需要分页）
	 * @return
	 */
	public String listSignedDocument() {
		//当前页数
		String str1 = request.getParameter("pageIndex");
		//每页显示的数量
		String str2 = request.getParameter("pageSize");
		int pageIndex = 1 ;
		try {
			pageIndex = Integer.parseInt(str1);
		} catch (Exception ex) {
			pageIndex = 1 ;
		}
		int pageSize = 30 ;
		try {
			pageSize = Integer.parseInt(str2);
		} catch ( Exception ex) {
			pageSize = 30 ;
		}
		//计算满足条件的文档总数
		int count = documentService.count_US_DO_FE_document(true);
		//计算总页面数
		int pageCount = 0 ;
		if ( count % pageSize == 0 ) {
			pageCount = count / pageSize ;
		} else {
			pageCount = count / pageSize + 1 ;
		}
		//调用业务层方法取得文档列表数据
		documentList = documentService.fetch_US_DO_FE_document(true,pageIndex,pageSize);
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("pageSize", pageSize);
		return SUCCESS ;
	}
	/**
	 * 医生列出所有未签名文档
	 * @return
	 */
	public String listUnSignedDocument() {
		String str1 = request.getParameter("pageIndex");
		String str2 = request.getParameter("pageSize");
		int pageIndex = 1 ;
		try {
			pageIndex = Integer.parseInt(str1);
		} catch (Exception ex) {
			pageIndex = 1 ;
		}
		int pageSize = 10 ;
		try {
			pageSize = Integer.parseInt(str2);
		} catch ( Exception ex) {
			pageSize = 10 ;
		}
		int count = documentService.count_US_DO_FE_document(false);
		int pageCount = 0 ;
		if ( count % pageSize == 0 ) {
			pageCount = count / pageSize ;
		} else {
			pageCount = count / pageSize + 1 ;
		}
		documentList = documentService.fetch_US_DO_FE_document(false,pageIndex,pageSize);
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("pageCount", pageCount);
		return SUCCESS ;
	}
	/**
	 * 操作员列出原始文档
	 * @return
	 */
	public String listOriginalDocument() {
		pdfDocumentList = documentService.fetch_OP_DO_FE_document();
		return SUCCESS ;
	}
	/**
	 * 操作员列出医院
	 * @return
	 */
	public List<Hospital> listHospital() {
		List<Hospital> hl = documentService.fetch_OP_HO_FE_hospital();
		return hl ;
	}
	/**
	 * 操作员列出指定医院的所有医生 
	 * @return
	 */
	public String fetchDoctor() {
		String hospitalUuid = request.getParameter("hospitalUuid");
		doctorList = documentService.fetch_OP_US_FE_user(hospitalUuid);
		return SUCCESS ;
	}
	/**
	 * 操作员分派文档
	 * @return
	 */
	public String assignDocument() {
		document.setCreateTime(new Timestamp(System.currentTimeMillis()));
		document.setReceivedTime(new Timestamp(Long.parseLong(receivedTime)));
		document.setSigned(false);
		document.setSignTime(null);
		document.setViewed(false);
		documentService.save_OP_DO_SA_document(document);
		return SUCCESS ;
	}
	/**
	 * 医生签名文档
	 * @return
	 */
	public String signDocument() {
		String alias = SecurityContextHolder.getAuthentication(request.getSession()).getName();
		String[] parameters = new String[]{alias,reason,location};
		documentService.sign_US_DO_SI_document(document, parameters);
		return SUCCESS ;
	}
	/**
	 * 取得指定ID的文档
	 * @return
	 */
	public String fetchDocument() {
		String uuid = request.getParameter("uuid");
		document = documentService.fetch_US_DO_FE_document(uuid);
		return SUCCESS ;
	}
}
